
// 对后台响应的数据做出处理
export const handleResponse = function (res, callback) {

  if (res.code === 0) {
    callback()
  }
  if (res.code === -1) {
    // Toast(res.msg)
  }
  if (res.code === -999) {
    // 清除 token
    setLocalStorage('token', '', 'string')
  }
}

// 计算输入的字符长度
export const calRealLength = function (str) {

  if (!str) return

  let reallength = 0
  for (let i = 0; i < str.length; i++) {
    let charCode = str.charCodeAt(i)
    if (charCode >= 0 && charCode <= 128) {
      reallength += 1
    } else {
      reallength += 2
    }
  }
  return reallength

}
//删除url中指定参数
export const delParam = function (name) {
  var loca = window.location;
  var baseUrl = loca.origin + loca.pathname;
  var query = loca.hash.substr(0);
  if (query.indexOf(name)>-1) {
      var obj = {}
      var arr = query.split("&");
      for (var i = 0; i < arr.length; i++) {
          arr[i] = arr[i].split("=");
          obj[arr[i][0]] = arr[i][1];
      };
      delete obj[name];
      var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
      return url
  };
}

// 向 localstorage 中存入数据  仅支持存字符串与对象格式
export const setLocalStorage = function (key, val, type) {
  if (!type) return
  if (type === 'string') {
    localStorage.setItem(key, val)
  }
  if (type === 'object') {
    localStorage.setItem(key, JSON.stringify(val))
  }

}

// 向 localstorage 中取数据
export const getLocalStorage = function (key, type) {
  if (!type) return
  if (type === 'string') {
    return localStorage.getItem(key)
  }
  if (type === 'object') {
    return JSON.parse(localStorage.getItem(key))
  }

}
// 向 localstorage 移除数据
export const removeStorage = function (key) {
    localStorage.removeItem(key)
}